﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Skyhook.AI.Animators
{
    public class Animator
    {
        public event OnAnimationFinish OnFinish = null;
        
        public bool IsFinished { get; private set; }
        
        public delegate void OnAnimationFinish();

        public Animator()
        {
            IsFinished = true;
        }

        public void StartAnimation()
        {
            CheckIfIsFinished();

            IsFinished = false;
            
            PrepareAnimation();
        }

        protected void CheckIfIsFinished()
        {
            if (!IsFinished)
            {
                throw new InvalidOperationException();
            }
        }

        protected virtual void PrepareAnimation()
        {
        }

        public void AbortAnimation()
        {
            if (!IsFinished)
            {
                DoActualAbortAnimation();
            }
        }

        protected virtual void DoActualAbortAnimation()
        {
            IsFinished = true;
        }

        protected virtual void FinishAnimation()
        {
            IsFinished = true;
        }

        public virtual void Update(float elapsedticks)
        {
            if (!IsFinished)
            {
                DoActualUpdate(elapsedticks);
            }
            else
            {
                CallOnFinish();
            }
        }

        protected virtual void DoActualUpdate(float elapsedticks)
        {
            IsFinished = true;
        }

        void CallOnFinish()
        {
            if (OnFinish != null)
            {
                OnFinish();
            }
        }
    }
}
